package com.shenhaofeng.app.widget.titlebar;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shenhaofeng.app.R;
import com.shenhaofeng.app.component.Title;


/**
 * 顶部标题栏
 * Created by Administrator on 2016/9/2.
 */
public class TitleBar extends LinearLayout implements Title {


    /**
     * 显示标题的View
     */
    private TextView titleTextView;

    /**
     * 显示左侧logo的View
     */
    private ImageView logoView;


    /**
     * 显示右侧菜单按钮的View
     */
    private ImageView menuView;



    public TitleBar(Context context) {
        super(context);
        init(context, null, 0);
    }


    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }


    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }


    /**
     * 初始View的属性已经初始化一些必要的资源
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    protected void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TitleBar, defStyleAttr, R.style.TitleBar);
        CharSequence titleText = ta.getText(R.styleable.TitleBar_titleText);
        int titlePx = ta.getDimensionPixelOffset(R.styleable.TitleBar_titleBarTextSize, 0);
        int titleSize = titlePx == 0 ? 20 : (int) (titlePx / getResources().getDisplayMetrics().scaledDensity);
        ColorStateList titleColor = ta.getColorStateList(R.styleable.TitleBar_titleBarTextColor);
        Drawable logoDrawable = ta.getDrawable(R.styleable.TitleBar_titleLogo);
        Drawable menuDrawable = ta.getDrawable(R.styleable.TitleBar_titleMenu);
        ta.recycle();
        //初始化布局参数
        TypedArray actionbarSizeTypedArray = context.obtainStyledAttributes(new int[]{android.R.attr.actionBarSize, android.R.attr.background});
        float height = (int) (getResources().getDisplayMetrics().density * 56);
        height = actionbarSizeTypedArray.getDimension(0, height);
        Drawable backgroundDrawable = actionbarSizeTypedArray.getDrawable(actionbarSizeTypedArray.getIndex(1));
        actionbarSizeTypedArray.recycle();
        int logoSize = (int) height;

        if (backgroundDrawable != null) {
            if (Build.VERSION.SDK_INT >= 16) {
                setBackground(backgroundDrawable);
            } else {
                setBackgroundDrawable(backgroundDrawable);
            }
        }

        LayoutParams logoLayoutParams = new LayoutParams(logoSize, logoSize);
        LayoutParams menuLayoutParams = new LayoutParams(logoSize, logoSize);
        LayoutParams titleLayoutParams = new LayoutParams(0, (int) height);
        titleLayoutParams.weight = 1;
        int padding = logoSize / 4;


        //标题View初始化
        titleTextView = new AppCompatTextView(context);
        titleTextView.setGravity(Gravity.CENTER);
        titleTextView.setTextColor(titleColor != null ? titleColor : ColorStateList.valueOf(Color.WHITE));
        titleTextView.setText(titleText);
        titleTextView.setTextSize(titleSize);

        logoView = new AppCompatImageView(context);
        logoView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        logoView.setPadding(padding, padding, padding, padding);
        logoView.setImageDrawable(logoDrawable);

        menuView = new AppCompatImageView(context);
        menuView.setVisibility(View.INVISIBLE);
        menuView.setPadding(padding, padding, padding, padding);
        menuView.setImageDrawable(menuDrawable);

        addView(logoView, logoLayoutParams);
        addView(titleTextView, titleLayoutParams);
        addView(menuView, menuLayoutParams);

    }


    /**
     * 设置菜单是否显示,默认不现实
     *
     * @param show
     */
    public void setMenuVisibility(boolean show) {
        menuView.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
    }


    /**
     * 设置标题和标题大小
     *
     * @param title
     * @param color
     */
    public void setTitle(CharSequence title, int color) {
        setTitle(title);
        setTitleColor(color);
    }


    /**
     * 设置标题
     *
     * @param title
     */
    @Override
    public void setTitle(CharSequence title) {
        titleTextView.setText(title);
    }


    /**
     * 设置标题颜色
     *
     * @param color
     */
    @Override
    public void setTitleColor(int color) {
        titleTextView.setTextColor(color);
    }


    /**
     * 设置监听器，处理logo和more按钮的单击事件
     *
     * @param listener
     */
    public void setTitleClickListener(final OnTitleClickListener listener) {
        if (listener != null) {
            menuView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onClickMore(v);
                }
            });
            logoView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onClickLogo(v);
                }
            });
        } else {
            menuView.setOnClickListener(null);
            logoView.setOnClickListener(null);
        }
    }


    public interface OnTitleClickListener {
        void onClickLogo(View view);


        void onClickMore(View view);
    }


}
